package cuongnh.project.japanesediary.vocabulary.ui.editor.grammar.page.general.section;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.beans.BeanProperties;
import org.eclipse.core.databinding.observable.ChangeEvent;
import org.eclipse.core.databinding.observable.IChangeListener;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.jface.databinding.swt.WidgetProperties;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.ui.forms.SectionPart;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;
import org.eclipse.wb.swt.ResourceManager;
import org.eclipse.wb.swt.SWTResourceManager;

import cuongnh.project.japanesediary.vocabulary.application.Activator;
import cuongnh.project.japanesediary.vocabulary.common.CommonServices;
import cuongnh.project.japanesediary.vocabulary.model.Grammar;
import de.ralfebert.rcputils.databinding.ChangeTracker;
import org.eclipse.swt.widgets.Label;

public class GrammarGeneralInfoSection extends SectionPart implements
		ModifyListener, IChangeListener {
	private DataBindingContext m_bindingContext;
	private Text txtGrammar;
	private Text txtVietnameseMeaning;
	private Text txtEnglishMeaning;
	private Text txtJapaneseMeaning;
	private Text txtVietnameseUsage;
	private Text txtEnglishUsage;
	private Text txtJapaneseUsage;
	private Grammar grammar;
	private CommonServices commonServices;
	private Section secGeneralInfo;
	private Text txtGrammarForm;

	// protected final DirtyFlag dirty = new DirtyFlag();

	/**
	 * Create the SectionPart.
	 * 
	 * @param parent
	 * @param toolkit
	 * @param style
	 * @wbp.parser.constructor
	 */
	public GrammarGeneralInfoSection(Composite parent, FormToolkit toolkit,
			int style) {
		super(parent, toolkit, style);
		secGeneralInfo = getSection();
		createClient(getSection(), toolkit);
	}

	public GrammarGeneralInfoSection(Composite parent, Grammar grammar,
			CommonServices commonServices, FormToolkit toolkit, int style) {
		super(parent, toolkit, style);
		this.grammar = grammar;
		this.commonServices = commonServices;
		createClient(getSection(), toolkit);
	}

	/**
	 * Fill the section.
	 */
	private void createClient(Section section, FormToolkit toolkit) {
		createToolBar(section);
		section.setText("General information");
		Composite container = toolkit.createComposite(section);

		section.setClient(container);
		container.setLayout(new GridLayout(2, false));
		CellEditor[] editors = new CellEditor[2];

		Group grpGrammar = new Group(container, SWT.NONE);
		GridData gd_grpGrammar = new GridData(SWT.FILL, SWT.FILL, false, false,
				2, 1);
		gd_grpGrammar.heightHint = 101;
		grpGrammar.setLayoutData(gd_grpGrammar);
		grpGrammar.setText("Grammar");
		toolkit.adapt(grpGrammar);
		toolkit.paintBordersFor(grpGrammar);
		grpGrammar.setLayout(new GridLayout(2, false));

		txtGrammar = new Text(grpGrammar, SWT.BORDER | SWT.WRAP | SWT.CENTER
				| SWT.MULTI);
		// txtVocabulary.setText(vocabulary.getVocabulary());
		txtGrammar.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		txtGrammar
				.setFont(SWTResourceManager.getFont("Segoe UI", 18, SWT.BOLD));
		txtGrammar.setEditable(false);
		txtGrammar.setBackground(SWTResourceManager
				.getColor(SWT.COLOR_TITLE_BACKGROUND));
		GridData gd_txtGrammar = new GridData(SWT.FILL, SWT.FILL, true, true,
				2, 1);
		gd_txtGrammar.widthHint = 219;
		gd_txtGrammar.heightHint = 95;
		txtGrammar.setLayoutData(gd_txtGrammar);
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);

		Group grpForms = new Group(container, SWT.NONE);
		grpForms.setLayout(new GridLayout(1, false));
		GridData gd_grpForms = new GridData(SWT.FILL, SWT.FILL, false, true, 1,
				1);
		gd_grpForms.widthHint = 333;
		gd_grpForms.heightHint = 107;
		grpForms.setLayoutData(gd_grpForms);
		grpForms.setText("Form");
		toolkit.adapt(grpForms);
		toolkit.paintBordersFor(grpForms);

		txtGrammarForm = new Text(grpForms, SWT.BORDER | SWT.WRAP | SWT.CENTER
				| SWT.MULTI);
		txtGrammarForm.setEditable(false);
		txtGrammarForm.setFont(SWTResourceManager.getFont("Segoe UI", 14,
				SWT.BOLD));
		txtGrammarForm.setForeground(SWTResourceManager
				.getColor(SWT.COLOR_WHITE));
		txtGrammarForm
				.setBackground(SWTResourceManager.getColor(102, 205, 170));
		GridData gd_txtGrammarForm = new GridData(SWT.FILL, SWT.FILL, true,
				true, 1, 1);
		gd_txtGrammarForm.widthHint = 392;
		gd_txtGrammarForm.heightHint = 87;
		txtGrammarForm.setLayoutData(gd_txtGrammarForm);
		// toolkit.paintBordersFor(table);

		Group grpMeaning = new Group(container, SWT.NONE);
		GridData gd_grpMeaning = new GridData(SWT.FILL, SWT.FILL, true, true,
				1, 1);
		gd_grpMeaning.heightHint = 85;
		gd_grpMeaning.widthHint = 300;
		grpMeaning.setLayoutData(gd_grpMeaning);
		grpMeaning.setText("Meaning");
		toolkit.adapt(grpMeaning);
		toolkit.paintBordersFor(grpMeaning);
		grpMeaning.setLayout(new GridLayout(1, false));

		TabFolder tabMeaning = new TabFolder(grpMeaning, SWT.NONE);
		GridData gd_tabMeaning = new GridData(SWT.FILL, SWT.FILL, true, true,
				1, 1);
		gd_tabMeaning.heightHint = 82;
		tabMeaning.setLayoutData(gd_tabMeaning);
		tabMeaning.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		toolkit.adapt(tabMeaning);
		toolkit.paintBordersFor(tabMeaning);

		TabItem tbtmVietnameseMeaning = new TabItem(tabMeaning, SWT.NONE);
		tbtmVietnameseMeaning.setImage(ResourceManager
				.getPluginImage("cuongnh.project.japanesediary.vocabulary",
						"icons/vietnam.jpg"));
		tbtmVietnameseMeaning.setText("Vietnamese");

		txtVietnameseMeaning = new Text(tabMeaning, SWT.BORDER | SWT.WRAP
				| SWT.V_SCROLL | SWT.MULTI);
		tbtmVietnameseMeaning.setControl(txtVietnameseMeaning);
		txtVietnameseMeaning.addModifyListener(this);

		TabItem tbtmEnglishMeaning = new TabItem(tabMeaning, SWT.NONE);
		tbtmEnglishMeaning.setImage(ResourceManager
				.getPluginImage("cuongnh.project.japanesediary.vocabulary",
						"icons/england.jpg"));
		tbtmEnglishMeaning.setText("English");

		txtEnglishMeaning = new Text(tabMeaning, SWT.BORDER | SWT.WRAP
				| SWT.V_SCROLL | SWT.MULTI);
		tbtmEnglishMeaning.setControl(txtEnglishMeaning);

		TabItem tbtmJapaneseMeaning = new TabItem(tabMeaning, SWT.NONE);
		tbtmJapaneseMeaning.setImage(ResourceManager.getPluginImage(
				"cuongnh.project.japanesediary.vocabulary", "icons/japan.jpg"));
		tbtmJapaneseMeaning.setText("Japanese");

		txtJapaneseMeaning = new Text(tabMeaning, SWT.BORDER | SWT.WRAP
				| SWT.V_SCROLL | SWT.MULTI);
		tbtmJapaneseMeaning.setControl(txtJapaneseMeaning);

		Group grpUsage = new Group(container, SWT.NONE);
		grpUsage.setText("Usage");
		GridData gd_grpUsage = new GridData(SWT.FILL, SWT.FILL, true, true, 2,
				1);
		gd_grpUsage.heightHint = 85;
		grpUsage.setLayoutData(gd_grpUsage);
		toolkit.adapt(grpUsage);
		toolkit.paintBordersFor(grpUsage);
		grpUsage.setLayout(new GridLayout(1, false));

		TabFolder tabUsage = new TabFolder(grpUsage, SWT.NONE);
		GridData gd_tabUsage = new GridData(SWT.FILL, SWT.FILL, true, true, 1,
				2);
		gd_tabUsage.heightHint = 99;
		gd_tabUsage.widthHint = 540;
		tabUsage.setLayoutData(gd_tabUsage);
		toolkit.adapt(tabUsage);
		toolkit.paintBordersFor(tabUsage);

		TabItem tbtmVietnameseUsage = new TabItem(tabUsage, SWT.NONE);
		tbtmVietnameseUsage.setImage(ResourceManager
				.getPluginImage("cuongnh.project.japanesediary.vocabulary",
						"icons/vietnam.jpg"));
		tbtmVietnameseUsage.setText("Vietnamese");

		txtVietnameseUsage = new Text(tabUsage, SWT.BORDER | SWT.WRAP
				| SWT.V_SCROLL | SWT.MULTI);
		tbtmVietnameseUsage.setControl(txtVietnameseUsage);

		TabItem tbtmEnglishUsage = new TabItem(tabUsage, SWT.NONE);
		tbtmEnglishUsage.setImage(ResourceManager
				.getPluginImage("cuongnh.project.japanesediary.vocabulary",
						"icons/england.jpg"));
		tbtmEnglishUsage.setText("English");

		txtEnglishUsage = new Text(tabUsage, SWT.BORDER | SWT.WRAP
				| SWT.V_SCROLL | SWT.MULTI);
		tbtmEnglishUsage.setControl(txtEnglishUsage);

		TabItem tbtmJapaneseUsage = new TabItem(tabUsage, SWT.NONE);
		tbtmJapaneseUsage.setImage(ResourceManager.getPluginImage(
				"cuongnh.project.japanesediary.vocabulary", "icons/japan.jpg"));
		tbtmJapaneseUsage.setText("Japanese");

		txtJapaneseUsage = new Text(tabUsage, SWT.BORDER | SWT.WRAP
				| SWT.V_SCROLL | SWT.MULTI);
		tbtmJapaneseUsage.setControl(txtJapaneseUsage);
		m_bindingContext = initDataBindings();
		ChangeTracker changeTracker = new ChangeTracker();
		changeTracker.trackTargetObservables(m_bindingContext);
		changeTracker.addChangeListener(this);
	}

	@Override
	public boolean isDirty() {
		// TODO Auto-generated method stub
		return super.isDirty();
	}

	@Override
	public void commit(boolean onSave) {
		// TODO Auto-generated method stub
		System.out.println("Commit from Section part!!!");
		commonServices.getGrammarDao().merge(grammar);
		super.commit(onSave);
	}

	@Override
	public void markDirty() {
		// TODO Auto-generated method stub
		super.markDirty();
	}

	@Override
	public void modifyText(ModifyEvent e) {
		super.markDirty();

	}

	@Override
	public void handleChange(ChangeEvent event) {
		super.markDirty();

	}

	private void createToolBar(Section section) {
		ToolBar toolbar = new ToolBar(section, SWT.FLAT);
		section.setTextClient(toolbar);

		ToolItem addItem = new ToolItem(toolbar, SWT.PUSH);
		addItem.setImage(Activator.getImageDescriptor("/icons/pencil.png")
				.createImage());
		addItem.setToolTipText("Edit");

		addItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				vocabularyMakeEditable(true);
			}
		});
	}

	private void vocabularyMakeEditable(boolean editable) {
		txtGrammar.setEditable(editable);
		txtGrammarForm.setEditable(editable);
	}

	protected DataBindingContext initDataBindings() {
		DataBindingContext bindingContext = new DataBindingContext();
		//
		IObservableValue observeTextTextObserveWidget = WidgetProperties.text(
				SWT.Modify).observe(txtGrammar);
		IObservableValue vocabularyVocabularyObserveValue = BeanProperties
				.value("grammar").observe(grammar);
		bindingContext.bindValue(observeTextTextObserveWidget,
				vocabularyVocabularyObserveValue, null, null);
		//
		IObservableValue observeTextTxtVietnameseMeaningObserveWidget = WidgetProperties
				.text(SWT.Modify).observe(txtVietnameseMeaning);
		IObservableValue vnMeaningVocabularyObserveValue = BeanProperties
				.value("vnMeaning").observe(grammar);
		bindingContext.bindValue(observeTextTxtVietnameseMeaningObserveWidget,
				vnMeaningVocabularyObserveValue, null, null);
		//
		IObservableValue observeTextTxtEnglishMeaningObserveWidget = WidgetProperties
				.text(SWT.Modify).observe(txtEnglishMeaning);
		IObservableValue enMeaningVocabularyObserveValue = BeanProperties
				.value("enMeaning").observe(grammar);
		bindingContext.bindValue(observeTextTxtEnglishMeaningObserveWidget,
				enMeaningVocabularyObserveValue, null, null);
		//
		IObservableValue observeTextTxtJapaneseMeaningObserveWidget = WidgetProperties
				.text(SWT.Modify).observe(txtJapaneseMeaning);
		IObservableValue jpMeaningVocabularyObserveValue = BeanProperties
				.value("jpMeaning").observe(grammar);
		bindingContext.bindValue(observeTextTxtJapaneseMeaningObserveWidget,
				jpMeaningVocabularyObserveValue, null, null);
		//
		IObservableValue observeTextTxtVietnameseUsageObserveWidget = WidgetProperties
				.text(SWT.Modify).observe(txtVietnameseUsage);
		IObservableValue vnUsageVocabularyObserveValue = BeanProperties.value(
				"vnUsage").observe(grammar);
		bindingContext.bindValue(observeTextTxtVietnameseUsageObserveWidget,
				vnUsageVocabularyObserveValue, null, null);
		//
		IObservableValue observeTextTxtEnglishUsageObserveWidget = WidgetProperties
				.text(SWT.Modify).observe(txtEnglishUsage);
		IObservableValue enUsageVocabularyObserveValue = BeanProperties.value(
				"enUsage").observe(grammar);
		bindingContext.bindValue(observeTextTxtEnglishUsageObserveWidget,
				enUsageVocabularyObserveValue, null, null);
		//
		IObservableValue observeTextTxtJapaneseUsageObserveWidget = WidgetProperties
				.text(SWT.Modify).observe(txtJapaneseUsage);
		IObservableValue jpUsageVocabularyObserveValue = BeanProperties.value(
				"jpUsage").observe(grammar);
		bindingContext.bindValue(observeTextTxtJapaneseUsageObserveWidget,
				jpUsageVocabularyObserveValue, null, null);
		//
		IObservableValue observeTextTxtGrammarFormObserveWidget = WidgetProperties
				.text(SWT.Modify).observe(txtGrammarForm);
		IObservableValue grammarFormGrammarObserveValue = BeanProperties.value(
				"grammarForm").observe(grammar);
		bindingContext.bindValue(observeTextTxtGrammarFormObserveWidget,
				grammarFormGrammarObserveValue, null, null);
		//
		return bindingContext;
	}
}
